﻿using King.Box.AdminWeb.Logic;
using King.Box.Utils.Global;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace King.Box.AdminWeb.Filter
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class LoginAttribute : ActionFilterAttribute
    {
        bool IsCheck { get; set; }
        private HomeService _homeService;

        #region 构造函数
        public LoginAttribute()
        {
            IsCheck = true;

            if (_homeService == null)
                _homeService = new HomeService();
        }
        public LoginAttribute(bool isCheck)
        {
            IsCheck = isCheck;

            if (_homeService == null)
                _homeService = new HomeService();
        }
        #endregion

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!IsCheck)
                goto BaseExecuting;

            if (AuthorizationCustomerService.IsLogin)
            {
                if (HttpContext.Current.Session[GlobalConst.Session_User] == null)
                    _homeService.AutoLogin();
            }
            else
            {
                var backurl = filterContext.HttpContext.Request.RawUrl;
                filterContext.Result = new RedirectResult("/home/login");
            }

            BaseExecuting:
            base.OnActionExecuting(filterContext);
        }
    }
}